package com.jwds.digital.bios.business.api.controller.dispatch;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.EscapeUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.jwds.digital.bios.business.core.constants.DeviceConstant;
import com.jwds.digital.bios.business.core.constants.DispatchConstant;
import com.jwds.digital.bios.business.core.enums.WarehouseClassification;
import com.jwds.digital.bios.business.core.infrastructure.dto.dispatch.DispatchInternalTransportReqDTO;
import com.jwds.digital.bios.business.core.infrastructure.dto.dispatch.DispatchParameterReqDTO;
import com.jwds.digital.bios.business.core.infrastructure.dto.dispatch.DispatchRevertReqDTO;
import com.jwds.digital.bios.business.core.infrastructure.vo.dispatch.DispatchInternalTransportRespVO;
import com.jwds.digital.bios.business.core.infrastructure.vo.feign.BasicStockBinRespVO;
import com.jwds.digital.bios.business.core.infrastructure.vo.feign.FlowBasicRespVO;
import com.jwds.digital.bios.business.service.service.dispatch.IDispatchInternalTransportService;
import com.jwds.tdc.framework.application.lib.BaseResponse;
import com.jwds.tdc.framework.application.lib.page.PageRequest;
import com.jwds.tdc.framework.application.lib.page.PageResponse;
import com.jwds.tdc.framework.cache.service.CacheService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 内部转运调度 前端控制器
 *
 * @author 林柏云
 * @date 2024-10-24
 */
@Slf4j
@Api(tags = "内部转运调度")
@RestController
@RequestMapping("/dispatch/internal/transport")
public class DispatchInternalTransportController {

    @Resource
    private IDispatchInternalTransportService service;

    @Autowired
    private CacheService cacheService;


    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询")
    public BaseResponse<DispatchInternalTransportRespVO> queryById(@PathVariable("id") Long id) {
        DispatchInternalTransportRespVO respVo = service.queryById(id);
        return BaseResponse.success(respVo);
    }

    @PostMapping("/list")
    @ApiOperation(value = "分页查询")
    public BaseResponse<PageResponse<DispatchInternalTransportRespVO>> page(@RequestBody PageRequest<DispatchInternalTransportReqDTO> pageRequest) {
        PageResponse<DispatchInternalTransportRespVO> respVoPage = service.page(pageRequest);
        return BaseResponse.success(respVoPage);
    }

    @PostMapping("/allBlastFurnace")
    @ApiOperation(value = "查询指定料仓数据数据")
    public BaseResponse<List<DispatchInternalTransportRespVO>> listBlastFurnace(String binName) {
        List<DispatchInternalTransportRespVO> respVoList = service.listBlastFurnace(binName);
        return BaseResponse.success(respVoList);
    }


    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除对象")
    public BaseResponse<?> delete(@PathVariable("id") Long id) {
        service.delete(id);
        return BaseResponse.success();
    }

    @GetMapping("originPointion/{originPointion}")
    @ApiOperation(value = "起点下拉框")
    public BaseResponse<List<String>> StartingPoint(@PathVariable String originPointion) {
        List<String> list = service.StartingPoint(originPointion);
        return BaseResponse.success(list);
    }
    @GetMapping("originDevice/{originDevice}")
    @ApiOperation(value = "起点设备下拉框")
    public BaseResponse<List<String>> destDevice(@PathVariable String originDevice) {
        List<String> respVoList = service.destDevice(originDevice);
        return BaseResponse.success(respVoList);
    }

    @GetMapping("/OriginDevice")
    @ApiOperation(value = "流程下拉框")
    public BaseResponse<List<String>> originDevice(@RequestBody DispatchInternalTransportReqDTO reqDTO) {
        List<String> respVoList = service.originDevice(reqDTO);
        return BaseResponse.success(respVoList);
    }
    @PostMapping("/Execute")
    @ApiOperation(value = "执行")
    public BaseResponse<DispatchInternalTransportRespVO> Execute(@RequestBody DispatchInternalTransportReqDTO reqDTO) {
        DispatchInternalTransportRespVO dispatchInternalTransportRespVO = service.execute(reqDTO);
        return BaseResponse.success(dispatchInternalTransportRespVO);
    }

    @GetMapping("/sinterBunker")
    @ApiOperation(value = "烧结燃料仓")
    public BaseResponse<List<DispatchParameterReqDTO>> sinterBunker() {
        List<DispatchParameterReqDTO> basicStockBinRespVOS = Lists.newArrayList();
        String str = cacheService.getString(DispatchConstant.REDIS_KEY + ":" + WarehouseClassification.SJRLC);
        if (null != str) {
            basicStockBinRespVOS = JSONUtil.toList(str, DispatchParameterReqDTO.class);
        }
        return BaseResponse.success(basicStockBinRespVOS);
    }


    @GetMapping("/finishedPellet")
    @ApiOperation(value = "球团成品仓")
    public BaseResponse<List<DispatchParameterReqDTO>> finishedPellet() {
        List<DispatchParameterReqDTO> basicStockBinRespVOS = Lists.newArrayList();
        String str = cacheService.getString(DispatchConstant.REDIS_KEY + ":" + WarehouseClassification.QTCPC);
        if (null != str) {
            basicStockBinRespVOS = JSONUtil.toList(str, DispatchParameterReqDTO.class);
        }
        return BaseResponse.success(basicStockBinRespVOS);
    }

    @GetMapping("/sinteredProduct")
    @ApiOperation(value = "烧结成品仓")
    public BaseResponse<List<DispatchParameterReqDTO>> sinteredProduct() {
        List<DispatchParameterReqDTO> basicStockBinRespVOS = Lists.newArrayList();
        String str = cacheService.getString(DispatchConstant.REDIS_KEY + ":" + WarehouseClassification.SJ34CPC);
        if (null != str) {
            basicStockBinRespVOS = JSONUtil.toList(str, DispatchParameterReqDTO.class);
        }
        return BaseResponse.success(basicStockBinRespVOS);
    }

    @GetMapping("pre/ingredient/warehouse")
    @ApiOperation(value = "预配料仓")
    public BaseResponse<List<DispatchParameterReqDTO>> PreIngredientWarehouse() {
        List<DispatchParameterReqDTO> basicStockBinRespVOS = Lists.newArrayList();
        String respVoList = cacheService.getString(DispatchConstant.REDIS_KEY+":" + WarehouseClassification.YPLC);
        if (null != respVoList){
            basicStockBinRespVOS = JSONUtil.toList(respVoList, DispatchParameterReqDTO.class);
        }
        return BaseResponse.success(basicStockBinRespVOS);
    }

    @GetMapping("dispatch/blockMine")
    @ApiOperation(value = "块矿缓冲仓加仓")
    public BaseResponse<List<DispatchParameterReqDTO>> dispatchBlockMine() {
        List<DispatchParameterReqDTO> basicStockBinRespVOS = Lists.newArrayList();
        String respVoList = cacheService.getString(DispatchConstant.REDIS_KEY+":" + WarehouseClassification.KKC);
        if (null != respVoList){
            basicStockBinRespVOS = JSONUtil.toList(respVoList, DispatchParameterReqDTO.class);
        }
        return BaseResponse.success(basicStockBinRespVOS);
    }






}
